package com.misakubex;

import com.hgh.model.Missile;
import com.hgh.model.Tank;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.concurrent.TimeUnit;

public class TankLocal {

    public Tank ta=new Tank();

    /**
     * 我方血量
     */
    public static int bleed = 3;

    public TankLocal(){
        ta.setX(10*Main.a);
        ta.setY(25*Main.a);
    }

    /**
     * 击杀敌机坦克数
     */
    public static int killTankNum = 0;

    class KeyMoniter implements KeyListener{
        /**
         * 使用flag来让坦克在按住键盘的时候只能走一步
         */
        boolean flag = true;

        @Override
        public void keyPressed(KeyEvent e) {//按下键事件a向左s向下d向右w向上
            if(flag){
                if(e.getKeyCode()==KeyEvent.VK_W||e.getKeyCode()==KeyEvent.VK_UP){
                    //向上
                    ta.setDirect('w');
                    ta.moveUp();
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                        Thread.yield();
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }else if(e.getKeyCode()==KeyEvent.VK_S||e.getKeyCode()==KeyEvent.VK_DOWN){
                    //设置我的坦克的方向,向下
                    ta.setDirect('s');
                    ta.moveDown();
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                        Thread.yield();
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }else if(e.getKeyCode()==KeyEvent.VK_A||e.getKeyCode()==KeyEvent.VK_LEFT){
                    //向左
                    ta.setDirect('a');
                    ta.moveLeft();
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                        Thread.yield();
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }else if(e.getKeyCode()==KeyEvent.VK_D||e.getKeyCode()==KeyEvent.VK_RIGHT){
                    //向右
                    ta.setDirect('d');
                    ta.moveRight();
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                        Thread.yield();
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }else if(e.getKeyCode()==KeyEvent.VK_J||e.getKeyCode()==KeyEvent.VK_SPACE){
                    //按下空格或J发射子弹
                    Missile missile = ta.emissionMissile();
                    if(missile!=null){
                        BTFrame.threadPoolExecutor.execute(missile);
                        BTFrame.addMissile(missile);
                    }
                }
                flag=false;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            flag=true;
        }

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub
        }

    }

}